package vn.edu.saigontech.ncms.web.user;

import vn.edu.saigontech.ncms.model.user.User;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateUserAction extends AbstractValidator {
	
	private Long id;
	private Long[] roles;
	private String username;
	private String password;
	private String firstName;
	private String lastName;
	private String email;

	public String check() {
		if(Checker.isNull(this.id)) {
			if(Checker.isEmpty(this.username)) {
				this.sendKeyError("validator.required", "username");
			}
			if(!Checker.isNull(UserService.INSTANCE.findByUsername(this.username))) {
				this.sendKeyError("validator.exist", "username");
			}
			if(Checker.isEmpty(this.password)) {
				this.sendKeyError("validator.required", "password");
			}
		}
		else {
			User uUser = UserService.INSTANCE.findByUsername(this.username);
			if(!Checker.isNull(uUser)) {
				if(!Checker.equals(this.id, uUser.getId())) {
					this.sendKeyError("validator.exist", "username");
				}
			}
		}
		if(Checker.isEmpty(this.firstName)) {
			this.sendKeyError("validator.required", "firstName");
		}
		if(!Checker.isEmpty(this.email)) {
			if(!Checker.isEmail(this.email)) {
				this.sendKeyError("validator.invalid", "email");
			}
		}
		if(this.roles == null || this.roles.length <= 0) {
			this.sendKeyError("validator.required", "roles");
		}
		return this.toSuccess();
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Long[] getRoles() {
		return roles;
	}

	public void setRoles(Long[] roles) {
		this.roles = roles;
	}
}
